Skip to content

Preserve jsx with custom module #7577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

nojaf
Copy link
Collaborator

@nojaf nojaf commented Jun 26, 2025

It was mentioned on the forum that -bs-jsx-preserve doesn't work in combination with -bs-jsx-module.

Sample to reproduce:

dune exec bsc -- ./tests/tests/src/jsx_preserve_custom_module.res       

@cristianoc PPX seems fine, and I believe the information gets lost before reaching js_dump.
Would you have any guess what is different for this code path?

Copy link

pkg-pr-new bot commented Jun 26, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7577

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7577

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7577

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7577

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7577

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7577

commit: 6de73df

@cknitt cknitt added this to the v12.1 milestone Jul 3, 2025
@cknitt cknitt requested a review from cristianoc July 3, 2025 09:22
@nojaf
Copy link
Collaborator Author

nojaf commented Jul 3, 2025

@cristianoc
Copy link
Collaborator

@nojaf could this be involved?

        let element_binding =
          match config.module_ |> String.lowercase_ascii with
          | "react" -> Lident "ReactDOM"
          | _generic -> module_access_name config "Elements"
        in

@cristianoc
Copy link
Collaborator

This example also has the issue, without using a custom module:

@@config({
  flags: ["-bs-jsx", "4", "-bs-jsx-preserve"],
})

module React = {
  type component<'props> = Jsx.component<'props>
  type element = Jsx.element

  external jsx: (component<'props>, 'props) => element = "jsx"

  type fragmentProps = {children?: element}

  external jsxFragment: component<fragmentProps> = "Fragment"
}

let _fragment = <> {Jsx.string("Hello, world!")} </>

@cristianoc
Copy link
Collaborator

Cutting down further, this seems the problematic line. So somehow defining the jsx external makes preserve mode unhappy.

module React = {
  include React
  
  // This is the problematic line:
  external jsx: (component<'props>, 'props) => element = "jsx"
}

@cristianoc
Copy link
Collaborator

cristianoc commented Jul 3, 2025

This with @module("react/jsx-runtime") makes it happy again.
There must be something in some transform or printing that relies on some details of the code generated when using @module in the external.

module React = {
  include React
  
  @module("react/jsx-runtime")
  external jsx: (component<'props>, 'props) => element = "jsx"
}

cristianoc added a commit that referenced this pull request Jul 3, 2025
cristianoc added a commit that referenced this pull request Jul 3, 2025
@cristianoc
Copy link
Collaborator

This with @module("react/jsx-runtime") makes it happy again. There must be something in some transform or printing that relies on some details of the code generated when using @module in the external.

module React = {
  include React
  
  @module("react/jsx-runtime")
  external jsx: (component<'props>, 'props) => element = "jsx"
}

Indeed the generated lambda is slightly different in the identifier used (where dump recognises "jsx"). Fixed in #7591

pull bot pushed a commit to orzogc/rescript-compiler that referenced this pull request Jul 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants